<!DOCTYPE html>
<body>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script type="module">

import {hid_test, trustedClick} from './resources/hid-test-utils.js';

const kTestVendorId = 0x1234;
const kTestProductId = 0xabcd;

hid_test(async (t, fake) => {
  const guid = fake.addDevice(fake.makeDevice(kTestVendorId, kTestProductId));
  fake.setSelectedDevice(guid);

  await trustedClick();
  const devices = await navigator.hid.requestDevice({filters: []});
  assert_true(devices instanceof Array);
  assert_equals(devices.length, 1);

  const device = devices[0];
  assert_true(device instanceof HIDDevice);
  assert_false(device.opened);

  await device.close();
  assert_false(device.opened);
}, 'Closing a closed device is not an error');

hid_test(async (t, fake) => {
  const guid = fake.addDevice(fake.makeDevice(kTestVendorId, kTestProductId));
  fake.setSelectedDevice(guid);

  await trustedClick();
  const devices = await navigator.hid.requestDevice({filters: []});
  assert_true(devices instanceof Array);
  assert_equals(devices.length, 1);

  const device = devices[0];
  assert_true(device instanceof HIDDevice);
  assert_false(device.opened);

  await device.open();
  assert_true(device.opened);

  device.close();
  assert_false(device.opened);

  await device.open();
  assert_true(device.opened);
}, 'Opening, closing, then opening again works');

hid_test(async (t, fake) => {
  const guid = fake.addDevice(fake.makeDevice(kTestVendorId, kTestProductId));
  fake.setSelectedDevice(guid);

  await trustedClick();
  const devices = await navigator.hid.requestDevice({filters: []});
  assert_true(devices instanceof Array);
  assert_equals(devices.length, 1);

  const device = devices[0];
  assert_true(device instanceof HIDDevice);
  assert_false(device.opened);

  fake.simulateConnectFailure();
  await promise_rejects_dom(t, 'NotAllowedError', device.open());
  assert_false(device.opened);

  await device.open();
  assert_true(device.opened);
}, 'Opening after an error works');

hid_test(async (t, fake) => {
  const guid = fake.addDevice(fake.makeDevice(kTestVendorId, kTestProductId));
  fake.setSelectedDevice(guid);

  await trustedClick();
  const devices = await navigator.hid.requestDevice({filters: []});
  assert_true(devices instanceof Array);
  assert_equals(devices.length, 1);

  const device = devices[0];
  assert_true(device instanceof HIDDevice);
  assert_false(device.opened);

  await device.open();
  return promise_rejects_dom(t, 'InvalidStateError', device.open());
}, 'Opening a device twice is an error');

hid_test(async (t, fake) => {
  const guid = fake.addDevice(fake.makeDevice(kTestVendorId, kTestProductId));
  fake.setSelectedDevice(guid);

  await trustedClick();
  const devices = await navigator.hid.requestDevice({filters: []});
  assert_true(devices instanceof Array);
  assert_equals(devices.length, 1);

  const device = devices[0];
  assert_true(device instanceof HIDDevice);
  assert_false(device.opened);

  const firstRequest = device.open();
  await promise_rejects_dom(t, 'InvalidStateError', device.open());
  await firstRequest;
}, 'Opening a device twice simultaneously is an error');

</script>
</body>
